package dao.impl;

import bean.Menu;
import dao.MenuDao;
import util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DBUtil implements MenuDao {

    public List<Menu> getMenuList(){

        List<Menu> menuList = new ArrayList<>();

        try {
            String sql = " select * from menu ";

            resultSet = query(sql);
            while (resultSet.next()){
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpMenuId(resultSet.getInt("upmenuId"));
                menu.setState(resultSet.getInt("state"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));

                menuList.add(menu);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }


        return menuList;
    }

    @Override
    public List<Menu> getRoleMenu(List<Integer> menuIds) {

        List<Menu> menus = new ArrayList<>();

        for(Integer menuId : menuIds) {
            List params = new ArrayList();
            params.add(menuId);

            try {
                String sql = " select * from menu where menuid=? ";

                resultSet = query(sql,params);

                if (resultSet.next()){
                    Menu menu = new Menu();

                    menu.setMenuId(resultSet.getInt("menuid"));
                    menu.setMenuName(resultSet.getString("menuname"));
                    menu.setUpMenuId(resultSet.getInt("upmenuId"));
                    menu.setState(resultSet.getInt("state"));
                    menu.setDesc(resultSet.getString("desc"));
                    menu.setUrl(resultSet.getString("url"));

                    menus.add(menu);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                closeAll();
            }

        }

        return menus;
    }

}
